﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReBasic.Helper
{
    public class StringHelper
    {
        public event MainFrame.InfoHandler StateEvent;
        public event MainFrame.ProcessHandler DetailProcessEvent;

        public int SearchList(string str, List<string> lst)
        {
            int ret = 0;
            foreach (string s in lst)
                if (s.Contains(str))
                    ret++;
            return ret;
        }

        public void SearchOccurrence(string s1, string s2, string s3)
        { 
            int cnt = 0;
            List<int> pidx = new List<int>();
            List<int> x1 = new List<int>();
            List<int> x2 = new List<int>();
            List<string> pool = new List<string>();
            List<string> p1 = new List<string>();
            List<string> p2 = new List<string>();
            #region Step 1
            FileStream fsr = new FileStream(MainFrame.SentPath, FileMode.Open);
            StreamReader sr = new StreamReader(fsr, Encoding.UTF8);
            while (!sr.EndOfStream)
            { 
                cnt++;
                string str = sr.ReadLine();
                if (str.Contains(s1))
                {
                    pool.Add(str);
                    pidx.Add(cnt);
                }
                if (cnt % 100000 == 0)
                    DetailProcessEvent((int)(100.0 * cnt / MainFrame.TotalSent));
            }
            sr.Close();
            fsr.Close();
            #endregion
            #region Step 2
            for (int i = 0; i < pool.Count; i++)
            { 
                string str = pool[i];
                int idx = pidx[i];
                if (str.Contains(s2))
                { 
                    p1.Add(str);
                    x1.Add(idx);
                }
            }
            #endregion
            #region Step 3
            for (int i = 0; i < p1.Count; i++)
            {
                string str = p1[i];
                int idx = x1[i];
                if (str.Contains(s3))
                {
                    p2.Add(str);
                    x2.Add(idx);
                }
            }
            #endregion
            #region Output
            FileStream fsw = new FileStream(MainFrame.DataPath + 
                "Select_" + s1 + "_" + s2 + "_" + s3 + ".txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fsw, Encoding.UTF8);
            for (int i = 0; i < x2.Count; i++)
            { 
                if (i > 0) sw.Write(":");
                sw.Write(x2[i]);
            }
            sw.WriteLine();
            foreach (string str in p2)
                sw.WriteLine(str);
            sw.Close();
            fsw.Close();
            #endregion
            StateEvent("Find complete.", true);
        }

        public List<string> SearchAllFile(string str, ref List<int> sent_r_idx)
        {
            int cnt = 0, cur = 0;
            StringBuilder sb;
            StreamReader sr, src;
            FileStream fsr, fsrc, fsw;
            StreamWriter sw;
            
            List<string> ret = new List<string>();
            StateEvent("\tSearching for sent_r......", false);
            fsr = new FileStream(MainFrame.SentPath, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            fsrc = new FileStream(MainFrame.ChunkPath, FileMode.Open);
            src = new StreamReader(fsrc, Encoding.UTF8);
            //fsw = new FileStream(MainFrame.DataPath + "debug_sh.txt", FileMode.Create);
            //sw = new StreamWriter(fsw, Encoding.UTF8);
            string blank_str = " " + str + " ";
            while (!sr.EndOfStream)
            {
                cur++;          //1 to n
                string rd = sr.ReadLine();
                String chunk = src.ReadLine();
                sb = new StringBuilder();
                sb.Append(' ').Append(rd).Append(' ');
                string use = sb.ToString();
                bool flag = false;
                string[] spt = chunk.Split('\t');
                foreach (string s in spt)
                    if (s == str)           //attr is the exactly chunk
                    {
                        flag = true;
                        break;
                    }
                //if (use.Contains(str)) 
                if (flag == true)           //find attr in chunk
                {
                    //sw.WriteLine(use);
                    string s = use.Replace(blank_str, " #### ").Trim();           //Replace attr by ####
                    ret.Add(s);
                    //ret.Add(use.Trim());
                    sent_r_idx.Add(cur);
                }
                if (++cnt % 100000 == 0)
                    DetailProcessEvent((int)(100.0 * cnt / MainFrame.TotalSent));
            }
            DetailProcessEvent(100);
            sr.Close();
            fsr.Close();
            //src.Close();
            //fsrc.Close();
            //sw.Close();
            //fsw.Close();
            StateEvent("\tSearching complete, with " + ret.Count + " sentences.", true);
            return ret;
        }

        public void SplitTripleSet(string filePath, string fileName)
        {
            int cnt = 0;
            FileStream fsr = new FileStream(filePath + fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fsr, Encoding.UTF8);
            FileStream fsw = new FileStream(filePath + "Set_1.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fsw, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                if (str.IndexOf("CONCEPTS") == 0)
                {
                    if (cnt++ != 0)
                    {
                        sw.Close();
                        fsw.Close();
                        fsw = new FileStream(filePath + "Set_"
                            + cnt.ToString() + ".txt", FileMode.Create);
                        sw = new StreamWriter(fsw, Encoding.UTF8);
                    }
                    StateEvent("Dealing with No." + cnt.ToString() + "...", false);
                }
                sw.WriteLine(str);
            }
            sr.Close();
            fsr.Close();
            sw.Close();
            fsw.Close();
            StateEvent("Finished.", true);
        }
    }
}
